package com.zbkj.crmeb.merchant.api.controller;

import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.chant.request.ChannelPartitionCouponRequest;
import com.zbkj.crmeb.chant.response.ChannelPartitionCouponResponse;
import com.zbkj.crmeb.chant.response.ChannelPartitionInfoResponse;
import com.zbkj.crmeb.chant.response.ChannelPartitionUserResponse;
import com.zbkj.crmeb.chant.service.ChannelPartitionCouponService;
import com.zbkj.crmeb.marketing.request.FrontStoreCouponUserRequest;
import com.zbkj.crmeb.marketing.response.FrontStoreCouponUserResponse;
import com.zbkj.crmeb.merchant.api.service.FrontCouponUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * @author lvmd
 * @Classname UserCouponController
 * @Description 用户-优惠券
 * @Date 2022-01-17
 */
@RestController("UserCouponController")
@RequestMapping("api/front/mer/coupon")
@Api(tags = "用户优惠券")
public class FrontUserCouponController extends ApiBaseController  {

    @Resource
    private FrontCouponUserService frontCouponUserService;
    @Autowired
    private ChannelPartitionCouponService channelPartitionCouponService;

    /**
     * 领取优惠券
     *
     * @param couponId  优惠券id
     * @return
     */
    @ApiOperation(value = "领取优惠券")
    @PostMapping(value = "/receive")
    public CommonResult<Object> receiveFrontCouponUser(@ApiParam(value = "优惠券发布id", required = true) @RequestParam Integer couponId) {
        return frontCouponUserService.receiveFrontCouponUser(couponId) ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 我的优惠券列表
     * @param request
     * @return
     */
    @ApiOperation(value = "我的优惠券列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<FrontStoreCouponUserResponse>> listFrontCouponUsers(@Validated FrontStoreCouponUserRequest request) {
        request.setStatus(request.getQueryType());
        return CommonResult.success(frontCouponUserService.listFrontCouponUsers(request));
    }

    @ApiOperation(value = "我的优惠券详情")
    @GetMapping(value = "/info")
    public CommonResult<FrontStoreCouponUserResponse> getFrontCouponUserInfo(@ApiParam(value = "优惠券id", required = true) @RequestParam Integer id){
        return CommonResult.success(frontCouponUserService.getFrontCouponUserInfo(id));
    }

    @ApiOperation(value = "瓜分券列表")
    @GetMapping(value = "/partition/list")
    public CommonResult<CommonPage<ChannelPartitionCouponResponse>> getFrontPartitionCouponList(PageParamRequest pageParamRequest, HttpServletRequest request) {
        return CommonResult.success(channelPartitionCouponService.getList(new ChannelPartitionCouponRequest(), pageParamRequest, getMerId(request)));
    }

    @ApiOperation(value = "领取瓜分券")
    @GetMapping(value = "/partition/receive")
    public CommonResult<Boolean> receivePartitionCoupon(Integer partitionId) {
        return channelPartitionCouponService.receivePartitionCoupon(partitionId) ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "瓜分券页面")
    @GetMapping(value = "/partition/page")
    public CommonResult<ChannelPartitionInfoResponse> getPartitionInfo(Integer id, Integer partitionId) {
        return CommonResult.success(channelPartitionCouponService.getPartitionInfo(id, partitionId));
    }

    @ApiOperation(value = "开红包")
    @GetMapping(value = "/partition/open/{partitionId}")
    public CommonResult<ChannelPartitionUserResponse> partitionOpen(@PathVariable Integer partitionId, HttpServletRequest servletRequest) {
        return CommonResult.success(channelPartitionCouponService.partitionOpen(partitionId, getMerId(servletRequest)));
    }

}
